var searchData = {size:5, start: 0};
$(document).ready(function(){
	$(".btn-border-white").click(modifyWare);
	
	$(".search-btm, #loadMore").click(function() {
		searchData.name = $("#nameSearch").val();
		loadWares();
	});
	loadWares();
});

/**
 * 点击搜索和分页加载数据
 */
function loadWares() {
	$.ajax({type:"POST", url: "/ware/wareContent.html",
		data: searchData,
		dataType: "html",
		success: function(html) {
			if(html) {
				searchData.start += searchData.size;
				$("#loadMore").before(html);
				// 重新绑定修改事件
				$(".btn-border-white").unbind("click");
				$(".btn-border-white").click(modifyWare);
			} else {
				$("#loadMore").html("没有更多数据啦！");
			}
		}, error : function() {
			showOpResultLayer("加载数据出错");
		}
	});
}

var wareId; // 记录当前的商品ID
var op; // 记录当前的商品ID
/**
 * 点击修改商品按钮触发
 */
function modifyWare() {
	wareId = $(this).attr("wid");
	op = $(this).attr("op");
	var html = [];
	switch(op) {
		case "name" :
			html.push('<div style="text-align: left;">原名称：');
			html.push($("#name_" + wareId).html());
			html.push("<br><br>");
			html.push('<label>新名称：</label><input id="name" style="width:70%;height:25px;">');
			html.push('</div>');
			break;
		case "price":
			var priceMarket = $("#priceMarket_" + wareId).html();
			if(!priceMarket || priceMarket == "--") {
				priceMarket = "";
			}
			html.push('<div style="text-align: left;">');
			html.push('<label>进货价：</label><input id="priceOriginal" class="ware-modify-input" value="'+$("#priceOriginal_" + wareId).val()+'">元<br>');
			html.push('<label>会员价：</label><input id="pricePromotional" class="ware-modify-input" value="'+$("#pricePromotional_" + wareId).html()+'">元<br>');
			html.push('<label>市场价：</label><input id="priceMarket" class="ware-modify-input" value="'+priceMarket+'">元<br>');
			html.push('<label>购买分：</label><input id="scoreBuy" class="ware-modify-input" value="'+$("#scoreBuy_" + wareId).html()+'">分<br>');
			html.push('<label>推广分：</label><input id="scorePromote" class="ware-modify-input" value="'+$("#scorePromote_" + wareId).html()+'">分<br>');
			html.push('<label>总积分：</label><input id="scoreTotal" class="ware-modify-input" value="'+$("#scoreTotal_" + wareId).html()+'" readonly>分<br>');
			html.push('</div>');
			break;
		case "inventory":
			html.push('<div style="text-align: left;">');
			html.push('<label>库存：</label><input id="inventory" class="ware-modify-input" value="'+$("#inventory_" + wareId).html()+'">件<br>');
			html.push('</div>');
			break;
		default:
			break;
	}
	showLayer(html.join(""));
}
/**
 * 绑定原价和会员价更改事件，更改后实时计算积分
 */
function bindPriceChange() {
	// 处理价格输入事件
	$("#pricePromotional,#priceOriginal").change(computeScore);
}

/**
 * 根据输入的价格计算积分
 */
function computeScore() {
	// 只有原价和优惠价都有值才计算积分
	var pricePromotional = $("#pricePromotional").val();
	var priceOriginal = $("#priceOriginal").val();
	if(!pricePromotional || !priceOriginal) {
		return false;
	}
	var profit = parseInt(pricePromotional) - parseInt(priceOriginal);
	var scoreBuy = 0;
	var scorePromote = 0;
	if(profit > 0) {
		// 计算利润积分
		profit = profit * score_exchange_rate;
		scoreBuy = profit * score_give_rate / 100;
		scorePromote = profit * score_promote_rate / 100;
	}
	// 根据设置计算
	$("#scoreBuy").val(scoreBuy);
	$("#scorePromote").val(scorePromote);
	$("#scoreTotal").val(scoreBuy + scorePromote);
}
/**
 * 显示弹出层，弹出层的内容，考虑合并
 */
function showLayer(content) {
	layer.open({
		content: content
		,shadeClose: false
    	,btn: ['取消', '修改']
    	,yes: function(index){
      		layer.close(index);
    	}
    	,no: function(index) {
    		var data = {};
    		data.id = wareId;
    		data.name = $("#name").val();
    		data.priceOriginal = $("#priceOriginal").val();
    		data.pricePromotional = $("#pricePromotional").val();
    		data.priceMarket = $("#priceMarket").val();
    		data.scoreBuy = $("#scoreBuy").val();
    		data.scorePromote = $("#scorePromote").val();
    		data.scoreTotal = $("#scoreTotal").val();
    		data.numberInventory = $("#inventory").val();
    		$.ajax({type:"POST", url: "/ware/save.html",
				data: data,
				dataType: "json",
				success: function(d) {
					if(d && d.code == 1) {
						showModify(data);
//						layer.close(index);
					} else {
						showOpResultLayer("修改商品信息失败！");
					}
				}, error : function() {
					showOpResultLayer("修改商品信息出错！");
				}
			});
    	}
    	,success: function () {
    		if(op && op == "price") {
    			bindPriceChange();
    		}
    	}
  	});
}

/**
 * 回显修改后的值，避免重新加载
 */
function showModify(data) {
	switch(op) {
		case "name" :
			$("#name_" + wareId).html(data.name);
			break;
		case "price":
			$("#priceOriginal_" + wareId).val(data.priceOriginal);
			$("#pricePromotional_" + wareId).html(data.pricePromotional);
			$("#priceMarket_" + wareId).html(data.priceMarket);
			$("#scoreBuy_" + wareId).html(data.scoreBuy);
			$("#scorePromote_" + wareId).html(data.scorePromote);
			$("#scoreTotal_" + wareId).html(data.scoreTotal);
			break;
		case "inventory":
			$("#inventory_" + wareId).html(data.numberInventory);
			break;
		default:
			break;
	}
}

/**
 * 弹出框，显示操作结果
 * @param content 结果内容
 */
function showOpResultLayer(content) {
	layer.open({
		content: content
    	,btn: ['关闭']
    	,yes: function(index){
      		layer.close(index);
    	}
	});
}
